package com.bawei.mall.product.service.impl;

import com.bawei.mall.product.domain.Po.Product;
import com.bawei.mall.product.domain.Po.ReadHistory;
import com.bawei.mall.product.domain.Po.SkuStockList;
import com.bawei.mall.product.domain.Vo.DetailVo;
import com.bawei.mall.product.mapper.CBrandMapper;
import com.bawei.mall.product.mapper.CProductMapper;
import com.bawei.mall.product.mapper.CReadHistoryMapper;
import com.bawei.mall.product.service.ICReadHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author：liurui
 * @Package：com.bawei.mall.search.service.impl
 * @Project：mall_cloud
 * @name：ReadHistoryServiceImpl
 */
@Service
public class CReadHistoryServiceImpl implements ICReadHistoryService {
    @Autowired
    private CReadHistoryMapper mapper;

    @Autowired
    private CProductMapper cProductMapper;

    @Autowired
    private CBrandMapper cBrandMapper;

    @Override
    public List<ReadHistory> list() {
        return mapper.list();
    }

    @Override
    public void create(ReadHistory readHistory) {
        String generatedString = generateRandomString(16);
        readHistory.setId(generatedString);
        mapper.create(readHistory);
    }

    private String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            stringBuilder.append(characters.charAt(index));
        }
        return stringBuilder.toString();
    }

    @Override
    public DetailVo detail(Integer id) {
        DetailVo detailVo = new DetailVo();
        //查询商品
        Product product= cProductMapper.detailVo(id);
        detailVo.setProduct(product);
        //查询品牌
        detailVo.setBrand(cBrandMapper.detail(product.getBrandId()));
        //查询sku
        List<SkuStockList> skuStockLists = mapper.findSku(id);
        detailVo.setSkuStockList(skuStockLists);

        detailVo.setCouponList(new ArrayList<>());
        detailVo.setProductAttributeList(mapper.findProductAttributeList(product.getProductAttributeCategoryId()));
        detailVo.setProductAttributeValueList(mapper.findProductAttributeValue(id));
        detailVo.setProductLadderList(mapper.findProductLadder(id));
        return detailVo;
    }

    @Override
    public void clear() {
        mapper.clear();
    }

    @Override
    public ReadHistory detail2(Integer productId) {
        return mapper.detail2(productId);
    }
}
